package com.boyunvision.controller;


import com.boyunvision.entity.iserestful.BaseForm;
import com.boyunvision.entity.iserestful.PushImageForm;
import com.boyunvision.entity.responsresult.BaseJson;
import com.boyunvision.entity.responsresult.BaseJsonFactory;
import com.boyunvision.entity.responsresult.DeleteRecForm;
import com.boyunvision.entity.responsresult.DeleteRecWsForm;
import com.boyunvision.entity.responsresult.IseRestfulBaseJson;
import com.boyunvision.entity.responsresult.PushFeatureForm;
import com.boyunvision.entity.responsresult.RetrieveFeatureForm;
import com.boyunvision.entity.responsresult.RetrieveImageForm;
import com.boyunvision.entity.responsresult.SelectImageForm;
import com.boyunvision.entity.responsresult.SelectNumForm;
import com.boyunvision.entity.responsresult.UpdateRecForm;
import com.boyunvision.entity.responsresult.UpdateRecWsForm;
import com.boyunvision.service.impl.IseServiceImpl;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path = {"Ise/cloudAtlas"})
public class IseHttpController {
  @Autowired
  private IseServiceImpl iseService;
  
  @RequestMapping(path = {"/createDB"})
  public BaseJson createDB(String db_uid, String fileList) {
    return this.iseService.createDB(db_uid, fileList);
  }
  
  @RequestMapping(path = {"/deleteDB"})
  public BaseJson deleteDB(String db_uid) {
    return this.iseService.deleteDB(db_uid);
  }
  
  @RequestMapping(path = {"/push_image"}, produces = {"application/json;charset=UTF-8"})
  public BaseJson pushImage(@Valid @ModelAttribute PushImageForm form, BindingResult result) {
    IseRestfulBaseJson iseRestfulBaseJson = formValid((BaseForm)form, result);
    if (iseRestfulBaseJson != null)
      return (BaseJson)iseRestfulBaseJson; 
    return this.iseService.pushImage(form);
  }
  
  @RequestMapping(path = {"/push_feature"}, produces = {"application/json;charset=UTF-8"})
  public BaseJson pushFeature(@Valid @ModelAttribute PushFeatureForm form, BindingResult result) {
    IseRestfulBaseJson iseRestfulBaseJson = formValid((BaseForm)form, result);
    if (iseRestfulBaseJson != null)
      return (BaseJson)iseRestfulBaseJson; 
    return this.iseService.pushFeature(form);
  }
  
  @RequestMapping(path = {"/retrieve_image"}, produces = {"application/json;charset=UTF-8"})
  public BaseJson retrieveImage(@Valid @ModelAttribute RetrieveImageForm form, BindingResult result) {
    IseRestfulBaseJson iseRestfulBaseJson = formValid((BaseForm)form, result);
    if (iseRestfulBaseJson != null)
      return (BaseJson)iseRestfulBaseJson; 
    return this.iseService.retrieveImage(form);
  }
  
  @RequestMapping(path = {"/retrieve_feature"}, produces = {"application/json;charset=UTF-8"})
  public BaseJson retrieveFeature(@Valid @ModelAttribute RetrieveFeatureForm form, BindingResult result) {
    IseRestfulBaseJson iseRestfulBaseJson = formValid((BaseForm)form, result);
    if (iseRestfulBaseJson != null)
      return (BaseJson)iseRestfulBaseJson; 
    return this.iseService.retrieveFeature(form);
  }
  
  @RequestMapping(path = {"/select_image"}, produces = {"application/json;charset=UTF-8"})
  public BaseJson selectImage(@Valid @ModelAttribute SelectImageForm form, BindingResult result) {
    IseRestfulBaseJson iseRestfulBaseJson = formValid((BaseForm)form, result);
    if (iseRestfulBaseJson != null)
      return (BaseJson)iseRestfulBaseJson; 
    return this.iseService.selectImage(form);
  }
  
  @RequestMapping(path = {"/select_num"}, produces = {"application/json;charset=UTF-8"})
  public BaseJson selectNum(@Valid @ModelAttribute SelectNumForm form, BindingResult result) {
    IseRestfulBaseJson iseRestfulBaseJson = formValid((BaseForm)form, result);
    if (iseRestfulBaseJson != null)
      return (BaseJson)iseRestfulBaseJson; 
    return this.iseService.selectNum(form);
  }
  
  @RequestMapping(path = {"/delete_rec"}, produces = {"application/json;charset=UTF-8"})
  public BaseJson deleteRec(@Valid @ModelAttribute DeleteRecForm form, BindingResult result) {
    IseRestfulBaseJson iseRestfulBaseJson = formValid((BaseForm)form, result);
    if (iseRestfulBaseJson != null)
      return (BaseJson)iseRestfulBaseJson; 
    return this.iseService.delectRec(form);
  }
  
  @RequestMapping(path = {"/delete_rec_ws"}, produces = {"application/json;charset=UTF-8"})
  public BaseJson deleteRecWs(@Valid @ModelAttribute DeleteRecWsForm form, BindingResult result) {
    IseRestfulBaseJson iseRestfulBaseJson = formValid((BaseForm)form, result);
    if (iseRestfulBaseJson != null)
      return (BaseJson)iseRestfulBaseJson; 
    return this.iseService.deleteRecWs(form);
  }
  
  @RequestMapping(path = {"/update_rec"}, produces = {"application/json;charset=UTF-8"})
  public BaseJson updateRec(@Valid @ModelAttribute UpdateRecForm form, BindingResult result) {
    IseRestfulBaseJson iseRestfulBaseJson = formValid((BaseForm)form, result);
    if (iseRestfulBaseJson != null)
      return (BaseJson)iseRestfulBaseJson; 
    return this.iseService.updateRec(form);
  }
  
  @RequestMapping(path = {"/update_rec_ws"}, produces = {"application/json;charset=UTF-8"})
  public BaseJson updateRecWs(@Valid @ModelAttribute UpdateRecWsForm form, BindingResult result) {
    IseRestfulBaseJson iseRestfulBaseJson = formValid((BaseForm)form, result);
    if (iseRestfulBaseJson != null)
      return (BaseJson)iseRestfulBaseJson; 
    return this.iseService.updateRecWs(form);
  }
  
  private IseRestfulBaseJson formValid(BaseForm form, BindingResult result) {
    if (result.hasErrors())
      return BaseJsonFactory.buildIseRestfulJson(75, "表单校验失败"+ result
          .getFieldError().getField()); 
    return null;
  }
}
